package com.itheima.demo1;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class ServerUdp {
    public static void main(String[] args) throws IOException {
        //定义接收端对象
        DatagramSocket datagramSocket =new DatagramSocket(10086);


        while (true) {
            byte[] bytes =new byte[8192];
            DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
            datagramSocket.receive(datagramPacket);

            InetAddress address = datagramPacket.getAddress();
            String port = "未知IP";
            if(address != null){
                port = address.getHostAddress();
            }

            byte[] data = datagramPacket.getData();
            String s = new String(data, 0, data.length);
            System.out.println(port+":"+s);
        }

    }
}
